package com.zt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zt.mapper.UserMapper;
import com.zt.pojo.User;
import com.zt.pojo.dto.LoginDto;
import com.zt.service.UserService;
import com.zt.util.ApiServiceExceptionEnum;
import com.zt.util.ResultVo;
import com.zt.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @Date 2025/5/9 18:57
 * @Author neko
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public ResultVo login(LoginDto dto) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUsername,dto.getUsername());
        User u = userMapper.selectOne(queryWrapper);
        if(u==null){
            return ResultVo.error(ApiServiceExceptionEnum.DATA_NOT_EXISTS);
        }
        if(!u.getPassword().equals(dto.getPassword())){
            return ResultVo.error(ApiServiceExceptionEnum.PASSWORD_ERROR);
        }
        String JwtToken = TokenUtils.createJwtToken(u.getId());
        redisTemplate.opsForValue().set("token",JwtToken);

        return ResultVo.success(200,"登陆成功",JwtToken);
    }
}
